/**************************************************************************//**
 * @file           main.c
 * @version        V1.0
 * $Revision:      1 $
 * $Date:          2017/01/04 $
 * @brief          Main function
 * @Author         Jeffrey Kang
 * @ContactInfo    chkang@esmt.com.tw
 *
 * @note
 *
 * Copyright (C) 1998 Elite Semiconductor Memory Technology Corp.
 * All rights reserved.
 *
 ******************************************************************************/


#include <REG52.h>
#include "system_FE81001.h"

long int xdata long_data _at_ 0x8000;
unsigned char xdata mul_data_char[4] _at_ 0x8000;
float xdata mul_data_fp _at_ 0x8000;



float aaa = 2.5 ;


/**
 * @brief       delay function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
void delay( void )
{
	unsigned int i;
	
	for( i = 0 ; i < 10 ; i++ ){}
}


/**
 * @brief       EINT0Init function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
void MFAInit(void)
{
	
	/*-------------------------------------------
	   16-bit mul   Unsigned Multiply
	-------------------------------------------*/
	/* unsigned mul data input */
	MFA->mul_ctrl = 0;                // unsigned multiply
	MFA->mulp[0] = 0xE8;					// L-byte
	MFA->mulp[1] = 0x03;						// H-byte  1000 = 0x3E8
	MFA->mulc[0] = 0xE8;						// L-byte
	MFA->mulc[1] = 0x03;            // H-byte  1000 = 0x3E8 
	delay();
	
	// Result   1000 * 1000  =  1000000(0x000F4240)
	mul_data_char[0] = MFA->prod[3];  //0x00
	mul_data_char[1] = MFA->prod[2];	//0x0F
	mul_data_char[2] = MFA->prod[1];	//0x42
	mul_data_char[3] = MFA->prod[0];	//0x40
	
		/*-------------------------------------------
	   16-bit mul   Signed Multiply
	-------------------------------------------*/
	/* signed mul data input */
	MFA->mul_ctrl = 1;                // signed multiply
	
	MFA->mulp[0] = 0xE8; // 1000
	MFA->mulp[1] = 0x03;
	MFA->mulc[0] = 0x48; // -3000
	MFA->mulc[1] = 0xF4;
	delay();
	mul_data_char[0] = MFA->prod[3];	//0xFF
	mul_data_char[1] = MFA->prod[2];	//0xD2
	mul_data_char[2] = MFA->prod[1];	//0x39
	mul_data_char[3] = MFA->prod[0];	//0x40

	
	/*-----------------------------------
	   FP mul
	-----------------------------------*/
	/* fp mul */
	MFA->fp_ctrl = 0;
	MFA->fp_mulp[0] = 0x00;
	MFA->fp_mulp[1] = 0x00;
	MFA->fp_mulp[2] = 0x20;
	MFA->fp_mulp[3] = 0x40;	// 2.5
	MFA->fp_mulc[0] = 0x00;
	MFA->fp_mulc[1] = 0x00;
	MFA->fp_mulc[2] = 0x20;
	MFA->fp_mulc[3] = 0x40;	// 2.5
	
	delay();
	
	mul_data_char[0] = MFA->fp_prod[3];	//0x40
	mul_data_char[1] = MFA->fp_prod[2];	//0xC8
	mul_data_char[2] = MFA->fp_prod[1];	//0x00
	mul_data_char[3] = MFA->fp_prod[0];	//0x00			6.25
	
	/*-----------------------------------
	   FP Log2
	-----------------------------------*/
	/* log data input */
	MFA->log_indat[0] = 0x00;
	MFA->log_indat[1] = 0x00;
	MFA->log_indat[2] = 0x28;
	MFA->log_indat[3] = 0x41;	// 10.5
	delay();
	mul_data_char[0] = MFA->log_outdat[3];	//0x40
	mul_data_char[1] = MFA->log_outdat[2];	//0x59
	mul_data_char[2] = MFA->log_outdat[1];	//0x1B
	mul_data_char[3] = MFA->log_outdat[0];	//0xBB

}


/**
 * @brief       main function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
int main( void )
{
	/* enable GPIO clock */
	PORT_PAPBPCPD_Clock_Enable();		// enable GPIOA GPIOB GPIOC GPIOD clock
	
	/* enable MFA clock */
	En_MFA_clk();     																 // enable MFA clock
	
	/* MFAInit function */
	MFAInit();

	/* enable global interrupt */
	INTC->int_gmask = 0x01;
	
	while(1);
}

